package com.example.bookapp.crntroller;

import com.example.bookapp.bean.PeopleBean;
import com.example.bookapp.dao.PepopleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/people")
public class PepopleController {
    @Autowired
    PepopleDao dao;
    @RequestMapping("/all")
    public Map<String,Object> peopleAll(){
        Map<String,Object> map=new HashMap<>();
        List<PeopleBean> all = dao.findAll();
        if(all!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",all);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @RequestMapping("/alls")
    @ResponseBody
    public Map<String,Object> peopleAlls(@RequestParam String phone){
        Map<String,Object> map=new HashMap<>();
        List<PeopleBean> all = dao.findAll();
        List<PeopleBean>list=new ArrayList<>();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getPhone().equals(phone)){
                list.add(all.get(i));
            }
        }
        if(list!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",list);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> peopleUpdate(@RequestParam String name,String newName,String phone){
        Map<String,Object> map=new HashMap<>();
        List<PeopleBean> all = dao.findAll();
        List<PeopleBean>list=new ArrayList<>();
        for(int i=0;i<all.size();i++){
            if(all.get(i).getPhone().equals(phone)){
                list.add(all.get(i));
            }
        }
        for(int i=0;i<list.size();i++){
            if(list.get(i).getPhone().equals(name)){
                list.get(i).setPhone(newName);
                dao.save(list.get(i));
            }
            if(list.get(i).getAddress().equals(name)){
                list.get(i).setAddress(newName);
                dao.save(list.get(i));
            }
            if(list.get(i).getEmail().equals(name)){
                list.get(i).setEmail(newName);
                dao.save(list.get(i));
            }
            if(list.get(i).getName().equals(name)){
                list.get(i).setName(newName);
                dao.save(list.get(i));
            }
            if(list.get(i).getQq().equals(name)){
                list.get(i).setQq(newName);
                dao.save(list.get(i));
            }
        }
        if(list!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",list);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}